package com.bingbaihanji.cloud.controller;

import cn.hutool.core.date.DateUtil;
import com.bingbaihanji.cloud.apis.PayFeignApi;
import com.bingbaihanji.cloud.entities.PayDTO;
import com.bingbaihanji.cloud.resp.ResultData;
import com.bingbaihanji.cloud.resp.ReturnCodeEnum;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author 冰白寒祭
 * @date 2024-03-07 14:17:22
 * @description //TODO
 */
@RestController
@Slf4j
public class OrderController {

    @Resource
    HttpServletRequest httpServletRequest;

    @Resource
    private PayFeignApi payFeignApi;
    private ResultData payInfo;

    @PostMapping("/feign/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO) {
        log.info("模拟本地新增订单,开启addpay支付微服务远程调用");
        var ResultData = payFeignApi.addPay(payDTO);
        return ResultData;

    }



//    @GetMapping("/feign/pay/get")

//    public ResultData getPayInfo() {
//        String parameter = httpServletRequest.getParameter("id");
//        System.out.println(parameter);
//        Integer id = Integer.valueOf(parameter);
//        log.info("支付微服务远程调用,按照id查询订单id为:{}", id);

    @GetMapping("/feign/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id) {
        log.info("支付微服务远程调用,按照id查询订单id为:{}",id);
        ResultData payInfo = null;
        try {
            System.out.println("调用开始:" + DateUtil.now());
            payInfo = payFeignApi.getPayInfo(id);
        } catch (Exception e) {
            System.out.println("调用结束:" + DateUtil.now());
            e.printStackTrace();
            payInfo.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }


        return payInfo;
    }

    @RequestMapping(value = "/feign/pay/mylb", method = RequestMethod.GET)
    public String myLb() {
        return payFeignApi.getLb();

    }


}
